#include <iostream>
#include <map>
#include <string>
#include <cstdio>
#include <cctype>

using namespace std;

struct {
	int x, y;
	bool set;
} result[20];

int dx[8] = {-1, -1, -1, 0, 0, 1, 1, 1};
int dy[8] = {-1, 0, 1, -1, 1, -1, 0, 1};

char a[50][50];
map<string, int> word;
int m, n, k;
int ntest;

bool inside(int x, int y)
{
	return ((0 <= x) && (x < m) && (0 <= y) && (y < n));
}

int main()
{
	cin >> ntest;
	while (ntest-- > 0)
	{
		scanf("\n");
		scanf("%d %d \n", &m, &n);
		for (int i = 0; i < m; i++)
		{
			for (int j = 0; j < n; j++)
			{
				scanf("%c", &a[i][j]);
				a[i][j] = (char) toupper(a[i][j]);
			}
			scanf("\n");
		}

		word.clear();
		scanf("%d\n", &k);
		for (int i = 0; i < k; i++) {
			string s;
			getline(cin, s);
			for (int j = 0; (unsigned) j < s.size(); j++) s[j] = (char) toupper(s[j]);
			word[s] = i;
			result[i].set = false;
		}

		for (int i = 0; i < m; i++)
			for (int j = 0; j < n; j++)
				for (int dir = 0; dir < 8; dir++)
				{
					int i1 = i, j1 = j;
					string curr = "";
					curr += a[i1][j1];

					while (true)
					{
						if (word.find(curr) != word.end())
						{
							int p = word[curr];
							if (result[p].set) break;
							result[p].set = true;
							result[p].x = i+1;
							result[p].y = j+1;
						}

						i1 += dx[dir];
						j1 += dy[dir];
						if (!inside(i1, j1)) break;
						curr += a[i1][j1];
					};
				}

		for (int i = 0; i < k; i++)
			cout << result[i].x << " " << result[i].y << endl;

		// This line is VERY IMPORTANT as I think the judge just check files matching exactly.
		if (ntest > 0) cout << endl;
	}
	return 0;
}
